package jiyun.com.yiyanmeng;


import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import base.BaseActivity;
import base.BaseFragment;
import presenter.ShouYePresenter;
import utils.LayoutUtil;
import view.ShouYeView;

public class ShouYeActivity extends BaseActivity<ShouYeView, ShouYePresenter> implements ShouYeView{


    private TabLayout tab;
    private String[] titles;
    private int[] tabImgs;
    private List<BaseFragment> fragments;
    private FragmentManager fragmentManager;
    private int oldPostion;

    @Override
    public void showToast(String msg) {

    }

    @Override
    protected void initData() {
        titles = new String[]{"题库", "商城", "课程", "论坛", "我"};
        fragments = new ArrayList<>();
        tabImgs = new int[]{R.drawable.main_tab_select_1,
                R.drawable.main_tab_select_2,
                R.drawable.main_tab_select_3,
                R.drawable.main_tab_select_4,
                R.drawable.main_tab_select_5};

        for (int i = 0; i <titles.length ; i++) {
            View view= LayoutUtil.findLayout(this,R.layout.item_tab);
            ImageView img = view.findViewById(R.id.item_tab_img);
            img.setImageResource(tabImgs[i]);
            TextView tv = view.findViewById(R.id.item_tab_tv);
            tv.setText(titles[i]);
            TabLayout.Tab tab = this.tab.newTab().setCustomView(view);
            this.tab.addTab(tab);
        }
        fragments.add(TiKuFragment.newInstance());
        fragments.add(ShoppingFragment.newInstance());
        fragments.add(KeChengFragment.newInstance());
        fragments.add(LunTanFragment.newInstance());
        fragments.add(MyFragment.newInstance());
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.shouye_fra,fragments.get(0))
                .add(R.id.shouye_fra,fragments.get(1))
                .add(R.id.shouye_fra,fragments.get(2))
                .add(R.id.shouye_fra,fragments.get(3))
                .add(R.id.shouye_fra,fragments.get(4))
                .show(fragments.get(0))
                .hide(fragments.get(1))
                .hide(fragments.get(2))
                .hide(fragments.get(3))
                .hide(fragments.get(4)).commit();




    }

    @Override
    protected void setListener() {
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position){
                    case 0:
                        switchFragment(position,oldPostion);
                        break;
                    case 1:
                        switchFragment(position,oldPostion);
                        break;
                    case 2:
                        switchFragment(position,oldPostion);
                        break;
                    case 3:
                        switchFragment(position,oldPostion);
                        break;
                    case 4:
                        switchFragment(position,oldPostion);
                        break;
                }
                oldPostion = position;

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void switchFragment(int position,int oldPostion) {
        if (position==oldPostion){
            return;
        }
        fragmentManager.beginTransaction().show(fragments.get(position)).hide(fragments.get(oldPostion))
                .commit();



    }

    @Override
    protected void initView() {
        tab = findViewById(R.id.shouye_tab);


    }

    @Override
    protected ShouYePresenter createPresenter() {
        return new ShouYePresenter();
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_shou_ye;
    }
}
